package state;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;

import sprite.Tower;
import sprite.WalkingSprite;
import data.Cell;
import data.GameView;
import data.Grid;
import data.Level;
import data.Sidebar;

public class PlayState extends AbstractGameState
{
    private Tower player;
    private WalkingSprite baddie;
    private Level level;
    private ArrayList<Tower> towers = new ArrayList<Tower>();
    private final static int CELLSIZE = 50;
    private int placingTower = -1;
    
    private Sidebar sidebar;
    
    
    public PlayState(GameView view)
    {
        super(view);
        
        Image image;
        
        this.sidebar = new Sidebar(this.getView().getBounds());
        
        this.level = new Level();
        Grid grid = new Grid();
        for (int x = 0; x < 16; x++) {
        	for (int y = 0; y < 11; y++) {
                grid.getCells()[x][y] = new Cell(true, 'X', x, y);
        	}
        }
        grid.getCell(0, 0).setWalkDirection('E');
        grid.getCell(0, 0).setBuildable(false);
        grid.getCell(1, 0).setWalkDirection('E');
        grid.getCell(1, 0).setBuildable(false);
        grid.getCell(2, 0).setWalkDirection('S');
        grid.getCell(2, 0).setBuildable(false);
        grid.getCell(2, 1).setWalkDirection('E');
        grid.getCell(2, 1).setBuildable(false);
        grid.getCell(3, 1).setWalkDirection('S');
        grid.getCell(3, 1).setBuildable(false);
        grid.getCell(3, 2).setWalkDirection('S');
        grid.getCell(3, 2).setBuildable(false);
        grid.getCell(3, 3).setWalkDirection('S');
        grid.getCell(3, 3).setBuildable(false);
        grid.getCell(3, 4).setWalkDirection('E');
        grid.getCell(3, 4).setBuildable(false);
        grid.getCell(4, 4).setWalkDirection('E');
        grid.getCell(4, 4).setBuildable(false);
        grid.getCell(5, 4).setWalkDirection('E');
        grid.getCell(5, 4).setBuildable(false);
        grid.getCell(6, 4).setWalkDirection('E');
        grid.getCell(6, 4).setBuildable(false);
        grid.getCell(7, 4).setWalkDirection('E');
        grid.getCell(7, 4).setBuildable(false);
        grid.getCell(8, 4).setWalkDirection('S');
        grid.getCell(8, 4).setBuildable(false);
        grid.getCell(8, 5).setWalkDirection('S');
        grid.getCell(8, 5).setBuildable(false);
        grid.getCell(8, 6).setWalkDirection('S');
        grid.getCell(8, 6).setBuildable(false);
        grid.getCell(8, 7).setWalkDirection('E');
        grid.getCell(8, 7).setBuildable(false);
        grid.getCell(9, 7).setWalkDirection('E');
        grid.getCell(9, 7).setBuildable(false);
        grid.getCell(10, 7).setWalkDirection('E');
        grid.getCell(10, 7).setBuildable(false);
        grid.getCell(11, 7).setWalkDirection('E');
        grid.getCell(11, 7).setBuildable(false);
        grid.getCell(12, 7).setWalkDirection('E');
        grid.getCell(12, 7).setBuildable(false);
        grid.getCell(13, 7).setWalkDirection('E');
        grid.getCell(13, 7).setBuildable(false);
        grid.getCell(14, 7).setWalkDirection('E');
        grid.getCell(14, 7).setBuildable(false);
        grid.getCell(15, 7).setWalkDirection('G');
        grid.getCell(15, 7).setBuildable(false);
        this.level.setGrid(grid);
        this.level.setStart(this.level.getGrid().getCell(0, 0));
        this.level.setEnd(this.level.getGrid().getCell(15, 7));
        image = new ImageIcon("images/player.png").getImage();
        this.player = new Tower();
        this.player.setPosition(400, 200);
        
        image = new ImageIcon("images/baddie.png").getImage();
        this.baddie = new WalkingSprite();
        this.baddie.setLevel(this.level);
        this.baddie.setCurrentPos(level.getStart());
        this.baddie.setImage(image);
        this.baddie.setPosition(0, 0);

        this.getView().startClock(50);
    }
    
    public void drawOn(Graphics g)
    {
        Rectangle bounds = this.getView().getBounds();
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, bounds.width, bounds.height);
                
        g.setColor(Color.BLACK);
        //Vertical lines
        for (int i = 0; i < bounds.width/CELLSIZE+1; i++) {
        	g.drawLine(i*CELLSIZE, 0, i*CELLSIZE, bounds.height);
        }
        //Horizontal lines
        for (int i = 0; i < bounds.height/CELLSIZE+1; i++) {
        	g.drawLine(0, i*CELLSIZE, bounds.width, i*CELLSIZE);
        }
        
        for (int x = 0; x < 16; x++) {
        	for (int y = 0; y < 11; y++) {
                this.level.getGrid().getCells()[x][y].drawOn(g);
        	}
        }
        
        this.sidebar.drawOn(g);       
        this.player.drawOn(g);
        this.baddie.drawOn(g);
        for (int i = 0; i < towers.size(); i++) {
        	this.towers.get(i).drawOn(g);
        }
    }
    
    public void clocktick()
    {
    	if (this.baddie.getHp() > 0) {
    		this.baddie.clocktick();

    		for (int i = 0; i < towers.size(); i++) {
    			if (towers.get(i).getAttackRectangle().intersects(baddie.getRect())) {
    				towers.get(i).setAttack(true);
    				baddie.decreaseHp(towers.get(i).getPower());
    			}
    			else {
    				if (towers.get(i).isAttack()) {
    					towers.get(i).setAttack(false);
    				}
    			}
    		}
    	}
    	else {
    		this.baddie.setPosition(4000, 4000);
    	}
    }
    
    public void handleMouseMoved(int mouseX, int mouseY)
    {
    	if (placingTower != -1) {
    		int row = mouseX/CELLSIZE;
    		int column = mouseY/CELLSIZE;
    		this.player.setImage(new ImageIcon("images/player" + placingTower + ".png").getImage());
    		this.player.setRadius(100);
    		this.player.setShowRadius(true);
    		this.player.setPosition(row*CELLSIZE, column*CELLSIZE);
    	}
        
    }
    public void handleMousePressed(MouseEvent e, int mouseX, int mouseY)
    {
    	if (e.getButton() == MouseEvent.BUTTON1) {
			for (int i = 0; i < towers.size(); i++) {
				towers.get(i).setShowRadius(false);
			}
    		if (placingTower != -1) {
    			if (mouseX < 800) {
    				int row = mouseX/CELLSIZE;
    				int column = mouseY/CELLSIZE;
    				if (this.level.getGrid().getCell(row, column).isBuildable()) {
    					Tower tower = new Tower();
    					tower.setImage(new ImageIcon("images/player" + placingTower + ".png").getImage());
    					tower.setPosition(row*CELLSIZE, column*CELLSIZE);
    					tower.setId(towers.size());
    					tower.setPower(50);
    					tower.setRadius(100);
    					tower.setShowRadius(true);
    					this.player.setShowRadius(false);
    					towers.add(tower);
    					placingTower = -1;
    					this.level.getGrid().getCell(row, column).setBuildable(false);
    				}
    			}
    		}
    		else {
    			int id = -1;
    			if (mouseX > 800) {
    				placingTower = this.sidebar.handleMousePressed(e, mouseX, mouseY);
    			}
    			else {
    				for (int i = 0; i < this.towers.size(); i++) {
    					int tempId = this.towers.get(i).handleMousePressed(e, mouseX, mouseY);
    					if (tempId != -1) {
    						id = tempId;
    					}
    				}
    				if (id != -1) {
    					this.sidebar.drawInfo(this.towers.get(id));
    				}
    				else {
    					this.sidebar.drawInfo(new Tower());
    				}
    			}
    		}
    	}
    }
}